<template>
	<view>
		<uni-popup ref="popup" type="bottom">
			<view class="bg-white ">
				<uni-section title="自定义Banner" type="circle">
					<template v-slot:right>
						<view @click="close">关闭</view>
					</template>
				</uni-section>


				<view class="padding-bottom-lg padding-sm" style="padding-bottom: 120rpx;">
					<view class="flex margin-bottom-lg" style="  flex-wrap: wrap;">
						<uni-data-checkbox multiple v-model="indexTopBar" max="8" :localdata="indexTopBarList"
							@change="xuanze" :map="{text:'meta',value:'id',text2:'title'}"></uni-data-checkbox>
					</view>
					<button @click="reset" class="login-btn cu-btn block bg-white lg round margin-bottom"
						plain>重置</button>
					<button @click="submitForm" class="login-btn cu-btn block bg-blue lg round">提交</button>
				</view>
			</view>
		</uni-popup>
		<updateAvatar ref="updateAvatar" @imgOk="uploadOk"></updateAvatar>
	</view>
</template>

<script>
	import constant from '@/utils/constant'
	import storage from '@/utils/storage'
	import updateAvatar from "@/pages/mine/avatar/index"
	import {
		doInsert
	} from '@/api/marketing/communication'
	import {
		doInsert as doInsertCustomer

	} from "@/api/marketing/customer/CustomerManagementApi";

	import {
		formatTime,
		dateToAge
	} from '@/utils/validate'
	import {
		getcommon,
		pictureProcessing,
	} from '@/utils/common'
	import {
		getOrSetJsonFile
	} from '../../api/customer';
	export default {
		components: {
			updateAvatar
		},
		data() {
			return {
				indexTopBarList: [],
				indexTopBar: []
			}
		},
		props: {
			customer: {}
		},
		mounted() {
			this.sourceList = getcommon("sourceList")
		},
		methods: {
			xuanze() {
				//对列表进行排序
				this.indexTopBarList.sort((x, y) => {
					let indexX = this.indexTopBar.indexOf(x.id);
					let indexY = this.indexTopBar.indexOf(y.id);

					// 对于不在 indexTopBar 中的元素，将其索引设置为 Infinity 以便排序时放在最后
					if (indexX === -1) indexX = Infinity;
					if (indexY === -1) indexY = Infinity;

					return indexX - indexY;
				});
				this.$forceUpdate()
			},
			open() {
				let indexTopBarList = storage.get('indexTopBar')
				this.indexTopBar = indexTopBarList.map(obj => obj.id);
				this.indexTopBarList = storage.get('menu')
				let asdf = []
				for (let s of this.indexTopBarList) {
					if (s.children.length > 0) {
						for (let s2 of s.children) {
							asdf.push(s2)
						}
					}
				}
				this.indexTopBarList = asdf

				this.xuanze()
				this.$refs.popup.open()
			},
			submitForm() {

				let newArray = this.indexTopBarList.filter(item => this.indexTopBar.includes(item.id));
				let newArray2 = this.indexTopBarList.filter(item => this.indexTopBar.includes(item.id)).map(item => item
					.id);
				//将newArray2  存到云端

				this.IndexTopBarJson(newArray2)
				this.$store.dispatch('commitSetSET_INDEX_TOP_BAR', newArray)
				this.$emit("refreshIndexTopBar")
				this.close()

			},
			async reset() {
				const {
					data
				} = await getOrSetJsonFile({
					filename: "IndexTopBar",
					type: "delete"
				})
				constant.initIndexTopBar().then(rrr => {
					console.log(rrr.length);
					this.$store.dispatch('commitSetSET_INDEX_TOP_BAR', rrr)
					this.$emit("refreshIndexTopBar")
					this.close()
				})


			},
			async IndexTopBarJson(data2) {
				let asdf = {
					data: JSON.stringify(data2),
					filename: "IndexTopBar",
				}
				const {
					data
				} = await getOrSetJsonFile(asdf)
				if (data) {
					// this.customWorkCloudList = JSON.parse(data)
				}
			},
			close() {
				Object.assign(this.$data, this.$options.data());
				this.$refs.popup.close()
			}
		}
	}
</script>

<style>
</style>